﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BYManageInfoSystem
{
    public partial class Frm_FeeDetail : Form
    {
        private DGBY.BLL.FeeCateTab m_bllFeeCateTab = new DGBY.BLL.FeeCateTab();
        private DGBY.BLL.FeeTab m_bllFeeTab = new DGBY.BLL.FeeTab();
        private DGBY.BLL.FacFeeTab m_bllFacFeeTab = new DGBY.BLL.FacFeeTab();
        private DGBY.BLL.FacFeeRelationTab m_bllFacFeeRelationTab = new DGBY.BLL.FacFeeRelationTab();
        
        private DataSet m_ds = new DataSet();
        private int m_nSelectFeeTabID = 0;

        public Frm_FeeDetail()
        {
            InitializeComponent();
        }

        private void InitCombox()
        {
            DataSet ds = m_bllFeeCateTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxFeeCate.DataSource = ds.Tables[0];
                this.comboBoxFeeCate.DisplayMember = "费用种类";
                this.comboBoxFeeCate.ValueMember = "FeeCateTabID";
                this.comboBoxFeeCate.SelectedIndex = 0;
            }
        }

        private void Frm_FeeDetail_Load(object sender, EventArgs e)
        {
            InitCombox();
        }

        private void ShowAllRecordInDataGridView()
        {
            dataGridViewPortList.DataSource = null;
            m_ds = m_bllFeeTab.GetFeeList("");
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dataGridViewPortList.DataSource = m_ds.Tables[0];
                dataGridViewPortList.Columns[0].Visible = false;
                dataGridViewPortList.Columns[1].Visible = false;
            }
        }

        private void buttonShowAllRecordPort_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView();
        }

        private void buttonAddPort_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.FeeTab modelFeeTab = new DGBY.Model.FeeTab();
                modelFeeTab.FeeName = this.textBoxFeeName.Text;
                modelFeeTab.FeeCateTabID = Convert.ToInt32(this.comboBoxFeeCate.SelectedValue.ToString(), 10);

                int nRetID = m_bllFeeTab.Add(modelFeeTab);
                DataSet ds = m_bllFacFeeTab.GetList("");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    DGBY.Model.FacFeeRelationTab modelFacFeeRelationTab = new DGBY.Model.FacFeeRelationTab();
                    modelFacFeeRelationTab.FeeTabID = nRetID;

                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        int nFacFeeTabID = Convert.ToInt32(ds.Tables[0].Rows[i]["FacFeeTabID"].ToString(), 10);

                        modelFacFeeRelationTab.FacFeeTabID = nFacFeeTabID;
                        m_bllFacFeeRelationTab.Add(modelFacFeeRelationTab);
                    }
                }

                m_nSelectFeeTabID = m_bllFeeTab.GetMaxId() - 1;
                ShowAllRecordInDataGridView();
                //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
           
        }

        private void buttonModifyPort_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.FeeTab modelFeeTab = m_bllFeeTab.GetModel(m_nSelectFeeTabID);
                if (modelFeeTab == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                modelFeeTab.FeeName = this.textBoxFeeName.Text;
                modelFeeTab.FeeCateTabID = Convert.ToInt32(this.comboBoxFeeCate.SelectedValue.ToString(), 10);

                m_bllFeeTab.Update(modelFeeTab);

                ShowAllRecordInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
           
        }

        private void buttonDelPort_Click(object sender, EventArgs e)
        {
            DGBY.Model.FeeTab modelFeeTab = m_bllFeeTab.GetModel(m_nSelectFeeTabID);
            if (modelFeeTab == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllFeeTab.Delete(m_nSelectFeeTabID);
            this.textBoxFeeName.Text = "";
            ShowAllRecordInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void dataGridViewPortList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strFeeTabID = this.dataGridViewPortList.Rows[this.dataGridViewPortList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectFeeTabID = Convert.ToInt32(strFeeTabID, 10);
            DGBY.Model.FeeTab modelFeeTab = m_bllFeeTab.GetModel(m_nSelectFeeTabID);
            if (modelFeeTab != null)
            {
                this.textBoxFeeName.Text = modelFeeTab.FeeName;
                this.comboBoxFeeCate.SelectedValue = modelFeeTab.FeeCateTabID;
            }
            else
            {
                this.textBoxFeeName.Text = "";
            }
        }

        private void dataGridViewPortList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewPortList.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }
    }
}
